<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="所在班级" prop="className">
        <el-select v-model="queryParams.className" clearable placeholder="选择班级">
          <el-option
            v-for="item in classManageList"
            :key="item.id"
            :label="item.className"
            :value="item.className">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="负责人" prop="teacherName">
        <el-select v-model="queryParams.className" clearable placeholder="选择班级">
          <el-option
            v-for="item in classManageList"
            :key="item.id"
            :label="item.className"
            :value="item.className">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="学生姓名" prop="studentName">
        <el-input
          v-model="queryParams.studentName"
          placeholder="请输入学生姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学生状态" prop="studentStatus">
        <el-select v-model="queryParams.studentStatus" clearable placeholder="请选择学生状态">
          <el-option
            v-for="item in studentManageList"
            :key="item.id"
            :label="item.className"
            :value="item.studentStatus">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="困难系数" prop="hardLevel">
        <el-select v-model="queryParams.hardLevel" clearable placeholder="请选择困难系数">
          <el-option
            v-for="item in hardLevel"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="模拟面试" prop="mockTest">
        <el-select v-model="queryParams.mockTest" clearable placeholder="模拟面试">
          <el-option
            v-for="item in allTest"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="结业面试" prop="endTest">
        <el-select v-model="queryParams.endTest" clearable placeholder="结业面试">
          <el-option
            v-for="item in allTest"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:focus:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:focus:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:focus:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:focus:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="focusList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <!--      <el-table-column label="重点关注ID" align="center" prop="id"/>-->
      <el-table-column label="负责老师" align="center" prop="teacherName"/>
      <!--      <el-table-column label="班级ID" align="center" prop="classId"/>-->
      <el-table-column label="所属班级" align="center" prop="className"/>
      <el-table-column label="学生姓名" align="center" prop="studentName"/>
      <el-table-column label="学生状态" align="center" prop="studentStatus"/>
      <el-table-column label="困难系数" align="center" prop="hardLevel"/>
      <el-table-column label="模拟面试" align="center" prop="mockTest"/>
      <el-table-column label="结业面试" align="center" prop="endTest"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleTrace(scope.row)"
            v-hasPermi="['system:focus:edit']"
          >跟踪记录
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:focus:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:focus:remove']"
          >删除
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleCancel(scope.row)"
            v-hasPermi="['system:focus:delete']"
          >不再关注
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改重点关注对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="负责老师" prop="teacherName">
          <el-input v-model="form.teacherName" placeholder="请输入负责老师"/>
        </el-form-item>
        <!--        <el-form-item label="班级ID" prop="classId">-->
        <!--          <el-input v-model="form.classId" placeholder="请输入班级ID" />-->
        <!--        </el-form-item>-->
        <el-form-item label="班级名称" prop="className">
          <el-select v-model="form.className" clearable placeholder="班级名称">
            <el-option
              v-for="item in classManageList"
              :key="item.id"
              :label="item.className"
              :value="item.className">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="学生姓名" prop="studentName">
          <el-select v-model="form.studentId" clearable placeholder="班级名称">
            <el-option
              v-for="item in studentManageList"
              :key="item.id"
              :label="item.studentName"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="学生状态" prop="studentStatus">
          <el-select v-model="form.studentStatus" clearable placeholder="学生状态">
            <el-option
              v-for="item in studentStatus"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="困难系数" prop="hardLevel">
          <el-select v-model="form.hardLevel" clearable placeholder="困难系数">
            <el-option
              v-for="item in hardLevel"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="模拟测试" prop="mockTest">
          <el-select v-model="form.mockTest" clearable placeholder="模拟测试">
            <el-option
              v-for="item in allTest"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="结业测试" prop="endTest">
          <el-select v-model="form.endTest" clearable placeholder="结业测试">
            <el-option
              v-for="item in allTest"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <el-dialog :title="traceHistorytitle" :visible.sync="traceOpen" width="500px" append-to-body>
      <el-timeline>
        <el-timeline-item
          v-for="item in traceHistoryList"
          :key="item.id"
          :timestamp="item.traceTime"
          placement="top">
          <el-card>
            <h4>跟踪人：{{item.teacher}}</h4>
            <p>{{item.description}}</p>
          </el-card>
        </el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>

<script>
import {listFocus, getFocus, delFocus, addFocus, updateFocus} from "@/api/system/focus";
import {listClassManage} from "@/api/system/classManage";
import {listStudentManage} from "@/api/system/studentManage";
import {getDicts} from "@/api/system/dict/data";
import {getTraceHistoryList, listTraceHistory} from "@/api/system/traceHistory";

export default {
  name: "Focus",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 重点关注表格数据
      focusList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //跟踪记录
      traceOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        teacherName: null,
        classId: null,
        className: null,
        studentId: null,
        studentName: null,
        studentStatus: null,
        hardLevel: null,
        mockTest: null,
        endTest: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      //学生管理列表
      studentManageList: [],
      //班级管理列表
      classManageList: {},
      //学生状态
      studentStatus: [],
      //困难系数
      hardLevel: [],
      //模拟测试结果或者结业测试
      allTest: [],
      //查询当前追踪学生的跟踪记录列表
      traceHistoryList: [],

      traceHistorytitle: '',
    };
  },
  created() {
    this.getList();
    this.getStudentList();
    this.getClassList();

    getDicts("student_status").then(result => {
      this.studentStatus = result.data;
    });
    getDicts("test_status").then(result => {
      this.allTest = result.data;
    });
    getDicts("hard_level").then(result => {
      this.hardLevel = result.data;
    });
  },
  methods: {
    /** 跟踪记录 通过学生的id去查所有的跟踪历史*/
    handleTrace(row) {
      const studentId = row.studentId;
      console.log(studentId);
      getTraceHistoryList(studentId).then(response => {
        this.traceHistoryList = response.data;
        // this.total = response.total;
        this.traceOpen = true;
        this.traceHistorytitle = "[" + row.studentName + "]跟踪历史";
        this.loading = false;

      })
    },
    /** 查询班级*/
    getClassList() {
      this.loading = true;
      listClassManage(this.queryParams).then(response => {
        this.classManageList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询所有学生信息 */
    getStudentList() {
      listStudentManage(this.queryParams).then(response => {
        this.studentManageList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询重点关注列表 */
    getList() {
      this.loading = true;
      listFocus(this.queryParams).then(response => {
        this.focusList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        teacherName: null,
        classId: null,
        className: null,
        studentId: null,
        studentName: null,
        studentStatus: null,
        hardLevel: null,
        mockTest: null,
        endTest: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加重点关注";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const id = row.id || this.ids
      getFocus(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改重点关注";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateFocus(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();

            });
          } else {
            addFocus(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();

            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除重点关注编号为"' + ids + '"的数据项？').then(function () {
        return delFocus(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/focus/export', {
        ...this.queryParams
      }, `focus_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
